package com.example.web.admin;

import com.example.model.ResponseData.ArticleResponseData;
import com.example.model.ResponseData.StaticticsBo;
import com.example.model.domain.Article;
import com.example.model.domain.Comment;
import com.example.service.IArticleService;
import com.example.service.ISiteService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/admin")
public class AdminController {
    private static final Logger logger= LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private ISiteService siteService;
    @GetMapping(value = {"","/index"})
    public String index(HttpServletRequest request){
        List<Article> articles=siteService.recentArticle(5);
        List<Comment> comments=siteService.recentComments(5);
        StaticticsBo statictics= siteService.getStatistics();
        request.setAttribute("comments",comments);
        request.setAttribute("articles",articles);
        request.setAttribute("statistics",statictics);
        return "back/index";
    }
    @Autowired
    private IArticleService articleServiceImpl;
    //向文章发表页跳转
    @GetMapping(value = "/article/toEditPage")
    public String newArticle(){
        return "back/article_edit";
    }
    //发表文章
    @PostMapping(value = "/article/publish")
    @ResponseBody
    public ArticleResponseData publishArticle(Article article){
        if(StringUtils.isBlank(article.getCategories())){
            article.setCategories("默认分类");
        }
        try{
            articleServiceImpl.publish(article);
            logger.info("发布成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            logger.error("文章发布失败，错误信息："+e.getMessage());
            return ArticleResponseData.fail();
        }
    }
    //跳转到后台文章列表页面
    @GetMapping(value = "/article")
    public String index(@RequestParam(value = "page",defaultValue = "1")int page,@RequestParam(value = "conunt",defaultValue = "10")int count,HttpServletRequest request){
        PageInfo<Article> pageInfo = articleServiceImpl.selectArticleWithPage(page,count);
        request.setAttribute("articles",pageInfo);
        return "back/article_list";
    }
    @GetMapping(value = "/article/{id}")
    public String editArticle(@PathVariable("id") String id,HttpServletRequest request){
        Article article=articleServiceImpl.selectArticleWithId(Integer.parseInt(id));
        request.setAttribute("contents",article);
        request.setAttribute("categories",article.getCategories());
        return "back/article_edit";
    }
    //修改文章
    @PostMapping(value = "/article/modify")
    @ResponseBody
    public ArticleResponseData modifyArticle(Article article){
        try{
            articleServiceImpl.updateArticleWithId(article);
            logger.info("文章更新成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            logger.info("文章更新失败，错误信息："+e.getMessage());
            return ArticleResponseData.fail();
        }
    }
    @PostMapping(value = "/article/delete")
    @ResponseBody
    public ArticleResponseData deleteArticle(@RequestParam int id){
        try{
            articleServiceImpl.deleteArticleWithId(id);
            logger.info("删除成功");
            return ArticleResponseData.ok();
        }catch (Exception e){
            logger.info("删除失败，错误信息："+e.getMessage());
            return ArticleResponseData.fail();
        }
    }
}